/**
 * Copyright (c) 2021, OSChina (oschina.net@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.linln.admin.app.service.task;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linln.common.config.properties.ProjectProperties;
import com.linln.common.utils.SpringContextUtil;
import org.gitlab4j.api.GitLabApi;

/**
 * Gitlab access instance
 *
 * @author Winter Lau<javayou@gmail.com>
 */
public class Gitlab {

    private static String gitlab_url = "https://git.betawm.com/";
    private static String access_token = "es3E-1BJDq-gyuh7yeca";
    private static String kooder_url = "http://172.18.12.44:8002/task/codes";

    public final static GitLabApi INSTANCE;

    static {
        ProjectProperties properties = null;
        try {
            properties = SpringContextUtil.getBean(ProjectProperties.class);
            System.out.println("init Gitlab config, properties=" + new ObjectMapper().writeValueAsString(properties));
        } catch (Exception e) {
        }

        gitlab_url = properties == null ? gitlab_url : properties.getGitlabUrl();// "https://git.betawm.com/";
        access_token = properties == null ? access_token : properties.getGitlabAccessToken();// "es3E-1BJDq-gyuh7yeca";
        kooder_url = properties == null ? kooder_url : properties.getKooderTaskUpdateUrl();

        INSTANCE = new GitLabApi(GitLabApi.ApiVersion.V4, gitlab_url, access_token);
        // Set the connect timeout to 1 second and the read timeout to 5 seconds
        int connectTimeout = 5000;
        int readTimeout = 20000;
        INSTANCE.setRequestTimeout(connectTimeout, readTimeout);
    }

    public static String getKooderUrl() {
        return kooder_url;
    }

}
